/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.component.cim.pusher;

import com.workcell.workdata.im.constant.Common;
import com.workcell.workdata.im.constant.MessageAction;
import com.workcell.workdata.im.entity.EventMessage;
import com.workcell.workdata.im.entity.Message;
import com.workcell.workdata.im.model.app.Origin;
import com.workcell.workdata.im.model.dto.TagMessage;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 通话接听操作同步消息
 */
@Component
public class WebrtcMessagePusher extends DefaultMessagePusher{

	/*
	 同意、拒绝事件，同步到其他设备的消息action映射
	 */
	private static final Map<String,String> actionMap = new HashMap<>();

	static {
		actionMap.put(MessageAction.ACTION_902,MessageAction.ACTION_928);
		actionMap.put(MessageAction.ACTION_903,MessageAction.ACTION_929);
		actionMap.put(MessageAction.ACTION_912,MessageAction.ACTION_930);
		actionMap.put(MessageAction.ACTION_913,MessageAction.ACTION_931);
	}

	/**
	 * 单人通话信令推送
	 * @param message a
	 */
	@Override
	public void push(Message message, Origin origin) {

		super.push(message,origin);

		if (!actionMap.containsKey(message.getAction())){
			return;
		}

		/*
		 * 给自己其他在线设备同步一条接听操作消息
		 * 排除当前设备 currentChannel
		 */
		Message syncMessage = new EventMessage();
		syncMessage.setId(message.getId());
		syncMessage.setSender(Common.SYSTEM_ID);
		syncMessage.setReceiver(message.getSender());
		syncMessage.setContent(message.getReceiver().toString());
		syncMessage.setAction(actionMap.get(message.getAction()));
		super.push(syncMessage,origin.getAppChannel());
	}

	/**
	 * 多人通话信令推送
	 * @param message
	 */
	public void push(long uid, TagMessage message, Origin origin) {

		this.signalRedisTemplate.push(message);

		if (!actionMap.containsKey(message.getAction())){
			return;
		}

		/*
		 * 给自己其他在线设备同步一条接听操作消息
		 * 排除当前设备 currentChannel
		 */
		Message syncMessage = new EventMessage();
		syncMessage.setSender(Common.SYSTEM_ID);
		syncMessage.setReceiver(uid);
		syncMessage.setContent(message.getTag());
		syncMessage.setAction(actionMap.get(message.getAction()));

		super.push(syncMessage,origin.getAppChannel());
	}

}
